define(['jquery', 'bootstrap', 'backend', 'table', 'form','cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格參數配置
            Table.api.init({
                extend: {
                    index_url: 'jdy/task/index',
                    add_url: 'jdy/task/add',
                    edit_url: 'jdy/task/edit',
                    del_url: 'jdy/task/del',
                    multi_url: 'jdy/task/multi',
                    table: 'jdy_task',
                }
            });

            var table = $("#table");

            var buttons = [];
            if (1) {
                //buttons.push({
                //    name: 'addtabs',
                //    title: __('變更工作進度'),
                //    classname: 'btn btn-xs btn-warning btn-dialog',
                //    icon: 'fa fa-signing',
                //    //url: 'jdy/maintenance/index2'
                //    url: 'jdy/maintenance/add3'
                //});
                buttons.push({
                    name: 'addtabs',
                    title: __('工作事项狀態變更記錄'),
                    classname: 'btn btn-xs btn-warning btn-addtabs',
                    icon: 'fa fa-server',
                    url: 'jdy/taskrecords/index'
                });
            }

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showColumns: true,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        //{field: 'anchang_id', title: __('Anchang_id')},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: buttons,
                            formatter: Table.api.formatter.operate
                        },
                        {field: 'anchang.name', title: __('Anchang.name'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'remind_date', title: __('Remind_date'), operate:'RANGE', addclass:'datetimerange', sortable: true},
                        {field: 'title', title: __('Title'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'price', title: __('Price'), operate:false},
                        {field: 'remind_type', title: __('Remind_type'), searchList: {"不提醒":__('不提醒'),"一般件單日提醒":__('一般件單日提醒'),"一般件多日提醒":__('一般件多日提醒'),"急件":__('急件')}, formatter: Table.api.formatter.normal},
                        {field: 'replay', title: __('Replay'), operate:false},
                        {field: 'quotation_no', title: __('Quotation_no'), operate:false},
                        {field: 'work_no', title: __('Work_no'), operate:false},
                        {field: 'task_status', title: __('類型'), searchList: {"一般作業":__('一般作業'),"緊急搶修(先請後扣)":__('緊急搶修(先請後扣)'),"緊急搶修(請了不扣)":__('緊急搶修(請了不扣)'),"待報價":__('待報價'),"已報價":__('已報價'),"待簽認":__('待簽認'),"待備料":__('待備料'),"待施工":__('待施工'),"未完工":__('未完工'),"待請款":__('待請款'),"已出單":__('已出單'),"已收款":__('已收款'),"已結案":__('已結案')}, formatter: Table.api.formatter.status},
                        {field: 'maintenanceuser', title: __('Maintenanceuser_id'), operate:false},
                        {field: 'handleuser', title: __('Handleuser_id'), operate:false},

                        //{field: 'baogu_start', title: __('Baogu_start'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'baogu_end', title: __('Baogu_end'), operate:'RANGE', addclass:'datetimerange'},

                        //{field: 'notifyuser_ids', title: __('Notifyuser_ids'), operate:false},
                        //{field: 'share_to_comm', title: __('Share_to_comm'), searchList: {"0":__('Share_to_comm 0'),"1":__('Share_to_comm 1')}, formatter: Table.api.formatter.normal},
                        //{field: 'notify_msg', title: __('Notify_msg')},
                        //{field: 'acdevice_ids', title: __('Acdevice_ids')},
                        //{field: 'baogu', title: __('Baogu'), searchList: {"六個月":__('六個月'),"一年":__('一年'),"兩年":__('兩年'),"三年":__('三年')}, formatter: Table.api.formatter.normal},
                        //{field: 'images', title: __('Images'), formatter: Table.api.formatter.images},
                        {field: 'luruman', title: __('Luruman'), operate:false},
                        //{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:false},
                        //{field: 'anchang.id', title: __('Anchang.id')},
                        //{field: 'anchang.building_id', title: __('Anchang.building_id')},
                        //{field: 'anchang.room_id', title: __('Anchang.room_id')},
                        //{field: 'anchang.name', title: __('Anchang.name')},
                        //{field: 'anchang.addr', title: __('Anchang.addr')},
                        //{field: 'anchang.remark', title: __('Anchang.remark')},
                        //{field: 'anchang.floor_no', title: __('Anchang.floor_no')},
                        //{field: 'anchang.contact1_id', title: __('Anchang.contact1_id')},
                        //{field: 'anchang.contact2_id', title: __('Anchang.contact2_id')},
                        //{field: 'anchang.companywindow_id', title: __('Anchang.companywindow_id')},
                        //{field: 'anchang.xianshi', title: __('Anchang.xianshi')},
                        //{field: 'anchang.xzq', title: __('Anchang.xzq')},
                        //{field: 'anchang.mapurl', title: __('Anchang.mapurl'), formatter: Table.api.formatter.url},
                        //{field: 'anchang.orderdate', title: __('Anchang.orderdate'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'anchang.fee', title: __('Anchang.fee')},
                        //{field: 'anchang.fee_type', title: __('Anchang.fee_type')},
                        //{field: 'anchang.pay_date', title: __('Anchang.pay_date'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'anchang.is_pay', title: __('Anchang.is_pay')},
                        //{field: 'anchang.create_time', title: __('Anchang.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'anchang.update_time', title: __('Anchang.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},

                    ]
                ]
            });

            // table选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                var myColumns=[];
                $.each(table.bootstrapTable('getHiddenColumns'), function(i, item) {
                    myColumns.push(item.field);
                });
                $.cookie('fa_jdy_task', JSON.stringify(myColumns), { expires: 365, path: '/'});
            });

            if($.cookie('fa_jdy_task')){
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_jdy_task')), function(i, item) {
                    table.bootstrapTable('hideColumn', item);
                });
            }

            // 批量审核
            $(document).on("click", ".btn-add2", function () {
                var url = 'jdy/maintenance/add2';
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: ["80%", "80%"],
                    callback: function (data) {

                    }
                };
                Fast.api.open(url, "添加此案場維修記錄", options);
            });

            // 為表格綁定事件
            Table.api.bindevent(table);
        },
        add: function () {

            $(document).on("dp.change", "#add-form .datetimepicker", function () {
                //$(this).parent().prev().find("input").trigger("change");
                var date = $("#c-remind_date").val();
                var date = new Date(date);
                console.log(date);
                var day = date.getDay();
                console.log(day);
                var strday = "";
                switch (day) {
                    case 0:
                        strday = '周日';
                        break;
                    case 1:
                        strday = '周一';
                        break;
                    case 2:
                        strday = '周二';
                        break;
                    case 3:
                        strday = '周三';
                        break;
                    case 4:
                        strday = '周四';
                        break;
                    case 5:
                        strday ='周五';
                        break;
                    case 6:
                        strday = '周六';
                        break;
                }
                console.log(strday);
                $("#c-weekinfo").html(strday);
            });

            Date.prototype.Format = function (fmt) {
                var o = {
                    "M+": this.getMonth() + 1, //月份
                    "d+": this.getDate(), //日
                    "H+": this.getHours(), //小時
                    "m+": this.getMinutes(), //分
                    "s+": this.getSeconds(), //秒
                    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                    "S": this.getMilliseconds() //毫秒
                };
                if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
                for (var k in o)
                    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                return fmt;
            };

            $(document).on("dp.change", "#c-baogu_start", function () {
                setbaogu_end();
            });

            $(document).on("change", "#c-baogu", function () {
                setbaogu_end();
            });

            setbaogu_end();

            function setbaogu_end() {
                var baogu_start = $("#c-baogu_start").val();
                var baogu = $("#c-baogu").val();
                baogu = parseInt(baogu);
                if (baogu < 1) {
                    baogu = 1;
                }

                var date = new Date(baogu_start);
                var enddate = new Date(date.setMonth(date.getMonth() + baogu));
                $("#c-baogu_end").val(enddate.Format("yyyy-MM-dd"));
            }

            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on("dp.change", "#edit-form .datetimepicker", function () {
                //$(this).parent().prev().find("input").trigger("change");
                var date = $("#c-remind_date").val();
                var date = new Date(date);
                console.log(date);
                var day = date.getDay();
                console.log(day);
                var strday = "";
                switch (day) {
                    case 0:
                        strday = '周日';
                        break;
                    case 1:
                        strday = '周一';
                        break;
                    case 2:
                        strday = '周二';
                        break;
                    case 3:
                        strday = '周三';
                        break;
                    case 4:
                        strday = '周四';
                        break;
                    case 5:
                        strday ='周五';
                        break;
                    case 6:
                        strday = '周六';
                        break;
                }
                console.log(strday);
                $("#c-weekinfo").html(strday);
            });

            Date.prototype.Format = function (fmt) {
                var o = {
                    "M+": this.getMonth() + 1, //月份
                    "d+": this.getDate(), //日
                    "H+": this.getHours(), //小時
                    "m+": this.getMinutes(), //分
                    "s+": this.getSeconds(), //秒
                    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                    "S": this.getMilliseconds() //毫秒
                };
                if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
                for (var k in o)
                    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                return fmt;
            };

            $(document).on("dp.change", "#c-baogu_start", function () {
                setbaogu_end();
            });

            $(document).on("change", "#c-baogu", function () {
                setbaogu_end();
            });

            setbaogu_end();

            function setbaogu_end() {
                var baogu_start = $("#c-baogu_start").val();
                var baogu = $("#c-baogu").val();
                baogu = parseInt(baogu);
                if (baogu < 1) {
                    baogu = 1;
                }

                var date = new Date(baogu_start);
                var enddate = new Date(date.setMonth(date.getMonth() + baogu));
                $("#c-baogu_end").val(enddate.Format("yyyy-MM-dd"));
            }

            $("#c-submit").on("click", function () {
                var that = this;
                Layer.confirm('確認修改麽？', {
                    btn: ['確認', '取消'] //按鈕
                }, function (index) {
                    $(that).closest("form").trigger("submit");
                    return true;
                }, function (index) {
                    layer.close(index);
                    return false;
                });
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});